package com.landmark;

import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JPanel;

public class ImagesFromFile extends JPanel {

    public void start() {
        HelperBean helperBean = Util.getHelperBean();
        String filePath = helperBean.getFilePath(); // images file
        String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1, filePath.lastIndexOf("."));
        thumbnailImagesFolder = "./outputs/" + fileName + "/ThumbnailImages/";
        largeImagesFolder = "./outputs/" + fileName + "/LargeImages/";
        try {
            Util.createDirectory(thumbnailImagesFolder);
            Util.createDirectory(largeImagesFolder);
            Util.createOutputFile(fileName + "Images");
            fwErrorFile = new FileWriter(Util.getHelperBean().getErrorFile());
            File file = new File(filePath);
            getLinesCount(file);
            processImagesFromURL(file);
        } catch (Exception e) {
            try {
                fwErrorFile.append(e.getMessage());
                fwErrorFile.flush();
            } catch (Exception e1) {
            }
        }
    }

    /**
     *
     * @param doc
     */
    private void loadAndSaveImagesFromUrl(String thumbnailImageUrl, String localStoragePath) {
        String imageName = thumbnailImageUrl.substring(thumbnailImageUrl.lastIndexOf("/"), thumbnailImageUrl.length());
        File imageFile = new File(localStoragePath + "/" + imageName);
        try {
            if (!imageFile.exists()) {
                Util.saveImage(thumbnailImageUrl, localStoragePath, null);
            }
        } catch (Exception e) {
            try {
                fwErrorFile.append(imageFile + "");
                fwErrorFile.append("\n");
                fwErrorFile.flush();
            } catch (Exception e1) {
            }
        }
    }

    private void processImagesFromURL(File inputFile) {
        lineCount = 0;
        pBar = new UMProgressBar();
        String readLine = null;

        if (inputFile.exists()) {
            //Scanner inputScan = new Scanner(inputFile);
            try {
                BufferedReader bf = new BufferedReader(new FileReader(inputFile));
                while ((readLine = bf.readLine()) != null) {
                    String[] line = readLine.split(",");
                    Util.getHelperBean().setCurrentCount(lineCount++);
                    if (line[0] != null && !line[0].trim().equals("")) {
                        loadAndSaveImagesFromUrl(line[0], thumbnailImagesFolder);
                    }
                    if (line.length > 1) {
                        if (line[1] != null && !line[1].trim().equals("")) {
                            loadAndSaveImagesFromUrl(line[1], largeImagesFolder);
                        }
                    }
                    if (Util.getHelperBean().getLineCount() == lineCount) {
                        Util.getHelperBean().setProcessingDone(true);
                        pBar.close();
                    }
                    EventQueue.invokeLater(pBar);
                }
            } catch (Exception e) {
                try {
                    fwErrorFile.append(readLine);
                    fwErrorFile.append("\n");
                    fwErrorFile.flush();
                    e.printStackTrace();
                } catch (Exception e1) {
                }
            }
        }

    }
    FileWriter fwErrorFile = null;
    int lineCount = 0;
    UMProgressBar pBar = null;

    protected void getLinesCount(File inputFile) throws Exception {
        if (inputFile.exists()) {
            BufferedReader bf = new BufferedReader(new FileReader(inputFile));
            while (bf.readLine() != null) {
                lineCount++;
            }
            Util.getHelperBean().setLineCount(lineCount);
        }
    }
    String thumbnailImagesFolder = "";
    String largeImagesFolder = "";
}
